package com.lz.fruitday.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.zhangwei.xutils.utils
 * @Description:
 * @date 15/10/17 上午10:46
 */

public class FileUtils {
    public static File getRootCache(Context context) {
        File rootFile = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            rootFile = context.getExternalCacheDir();
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                rootFile = new File(Environment.getExternalStorageDirectory() + File.separator + AppConfig.ROOT_CACHE);
            } else {
                rootFile = new File(Environment.getDataDirectory() + File.separator + context.getPackageName());
            }
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
        }
        return rootFile;
    }

    //sdk/qianfeng/imageCache

    public static String getSaveBitmapPath(Context context) {
        String imagePath = getRootCache(context) + AppConfig.IMAGE_CACHE;
        if (!new File(imagePath).exists()) {
            boolean mkdirs = new File(imagePath).mkdirs();
        }
        return imagePath;
    }
    /**
     * 获取数据库路径
     *
     * @param context
     * @return
     */
    public static String getDBPath(Context context) {
        String dbPath = getRootCache(context) + File.separator + AppConfig.DB_CACHE;
        if (!new File(dbPath).exists()) {
            boolean mkdirs = new File(dbPath).mkdirs();
        }

        return dbPath;
    }


    public static File getDbFile(Context context) {
        String dbPath = getRootCache(context) + File.separator + AppConfig.DB_DIR;
        File dbFile = new File(dbPath);
        if (!dbFile.exists()) {
            dbFile.mkdir();
        }
        return dbFile;
    }

}
